<!--
- Author:  LiShibin.
- Date:    2018/4/8 0008.
- File:    vip.
-->
<template>
  <div class="vipmenber">
    <img src="../../assets/images/viptitle.png">
    <h1>恭喜您注册成功</h1>
    <p>请支付相关费用激活账户</p>
    <button class="vipbtn" @click="buy2">立即支付</button>
    <button style="margin-left: 5px;" class="vipbtn" @click="signOut">切换账号</button>
    <mt-actionsheet
      :actions="actions"
      v-model="sheetVisible">
    </mt-actionsheet>
  </div>
</template>

<script>
  import payPop from '../../components/payPop'
  import {Actionsheet} from 'mint-ui'
  import * as request from '../../request'
  import config from '../../config'

  export default {
    async beforeRouteEnter(to, from, next) {
      try {
        console.log('config', config.goodsCommonId1)
        let goods1 = await request.getGoodsCommonInfo(config.goodsCommonId1)
        let goods3 = await request.getGoodsCommonInfo(config.goodsCommonId3)
        next(vm => {
          vm.actions = [{
            name: parseInt(goods1.goods_price) + '(会员费)',
            method: vm.buy1
          }, {
            name: parseInt(goods3.goods_price) + '(预付款)',
            method: vm.buy2
          }]
        })
      } catch (e) {
        next()
      }
      // request.getGoodsCommonInfo().then(data => {
      //   if (data.is_active == '1') {
      //     next(vm => {
      //       vm.memberInfo = data
      //     })
      //   } else {
      //     next({name: 'member-tobeVip'})
      //   }
      // }).catch(e => {
      //   next()
      // })
    },
    data() {
      return {
        sheetVisible: false,
        actions: [{
          name: '会员费',
          method: this.buy1
        }, {
          name: '预付款',
          method: this.buy2
        }]
      }
    },
    components: {
      'mt-actionsheet': Actionsheet
    },
    methods: {
      signOut() {
        this.$storage.delKey()
        this.$storage.setAuthed(false)
        this.$storage.delMemberInfo()
        this.$utils.delCookie('key')
        this.$router.replace({name: 'my'})
      },
      canclePay() {
      },
      switchSheetShow(boolValue) {
        this.sheetVisible = boolValue
      },
      buy1() {
        this.pay(this.$config.goodsId1) // 会员费的goods_id为285
        this.switchSheetShow(false)
      },
      buy2() {
        this.pay(this.$config.goodsId3) // 预付款全额的goods_id为287
        this.switchSheetShow(false)
      },
      async pay(goodsId) {
        this.$indicator.open('处理中...')
        let cartId = await this.$request.addGoodsToCart(goodsId, 1) // 加入购物车
        let param = {
          cart_id: [cartId],
          ifcart: 1,
          is_balance: 0,
          is_integral: 0,
          order_note: '',
          pay_name: 'online',
          self: '0',
          is_needship: 0
        }
        let order = await this.$request.submitOrder(param) // 生成订单
        this.$indicator.close()
        payPop({paySn: order.pay_sn, orderId: order.order_id}, this.canclePay)
      }
    }
  }
</script>

<style>

</style>